/**
  ******************************************************************************
  * @file    sh30f9xx_sa0_led.c
  * @author  sinowealth
  * @version V1.1.0
  * @date    2021-01-07  
  * @brief   This file provide APIs for using LED module
  *         
  * @verbatim
  *
  *          ===================================================================
  *                                  How to use this driver
  *          ===================================================================

  *  @endverbatim
  *
  ******************************************************************************
  * @attention
  *
  * SINOWEALTH IS SUPPLYING THIS SOFTWARE FOR USE EXCLUSIVELY SH_ON SINOWEALTH'S 
  * MICROCONTROLLER PRODUCTS. IT IS PROTECTED UNDER APPLICABLE COPYRIGHT LAWS. 
  * THIS SOFTWARE IS FOR GUIDANCE IN ORDER TO SAVE TIME. AS A RESULT, SINOWEALTH 
  * SHALL NOT BE HELD LIABLE FOR ANY DIRECT, INDIRECT OR CONSEQUENTIAL DAMAGES 
  * WITH RESPECT TO ANY CLAIMS ARISING FROM THE CONTENT OF SUCH FIRMWARE AND/OR
  * THE USE MADE BY CUSTOMERS OF THE CODING INFORMATION CONTAINED HEREIN IN 
  * CONNECTION WITH THEIR PRODUCTS.
  *
  * <h2><center>&copy; COPYRIGHT 2021 Sinowealth</center></h2>
  *
  ******************************************************************************
  */

/* Includes ------------------------------------------------------------------*/
#include "sh30f9xx_sa0_led.h"

/** @addtogroup SH30F9xx_sa0_libcfg_StdLib_Driver
  * @{
  */

/* LED Module----------------------------------------------------------*/
/** @defgroup LED_MODULE  LED 
  *  LED Calculate Mode
  * @{
  */

/**
  * @brief  Fills each LED_InitStruct member with its default value. 
  * @param  LED_InitStruct: pointer to a LED_InitTypeDef structure which will 
  *         be initialized.
  * @retval None
  */
void LED_StructInit(LED_InitTypeDef *LED_InitStruct)
{
    /* for COMCR register */
    /* Initialize  scanf COM select */
    LED_InitStruct->LedComSEL = LED_COMCR_COM0;

    /* Initialize  COM or IO select */
    LED_InitStruct->ComOrIoSEL = LED_COMCR_IO;

    /* Initialize LED SEG width select */
    LED_InitStruct->LedSEGWid = LED_COMCR_FIX;

    /* Initialize LED COM  width select */
    LED_InitStruct->LedComWidthSEL = 0X00;

    /* for AUCR register */
    /* Initialize led dead zone width seclet */
    LED_InitStruct->LedAuDeadWid = 0X00;

    /* Initialize led continr COM seclet */
    LED_InitStruct->LedAuComSEL = LED_AUCR_COM0;

    /* Initialize led continr scanf continu or stop */
    LED_InitStruct->LedAuScanfCONT = LED_AUCR_GOON;

    /* Initialize led continr scanf mode seclet */
    LED_InitStruct->LedAuScanfMode = LED_AUCR_SINGLE;
}

/** 
  * @brief  Initializes the LED driver mould peripheral according to the specified 
  *         parameters in the InitCfg.
  * @param  InitCfg,pointer to a LED_InitTypeDef structure that
  *         contains the configuration information for the specified LED peripheral.
  * @retval None
  */

void LED_Init(const LED_InitTypeDef *InitCfg)
{
    LED->COMCR.BIT.COMSEL = InitCfg->LedComSEL;
    LED->COMCR.BIT.COMIO  = InitCfg->ComOrIoSEL;
    LED->COMCR.BIT.MODE   = InitCfg->LedSEGWid;
    LED->COMCR.BIT.DISCOM = InitCfg->LedComWidthSEL;

    LED->AUCR.BIT.DZ   = InitCfg->LedAuDeadWid;
    LED->AUCR.BIT.COM  = InitCfg->LedAuComSEL;
    LED->AUCR.BIT.AUMD = InitCfg->LedAuScanfCONT;
    LED->AUCR.BIT.AUEN = InitCfg->LedAuScanfMode;
}

/**
  * @brief  Clear the LED single pending flags.
  * @param  LED_Flag: specifie the flag to clear.
  *         This parameter can be any combination of the following values:
  *            @arg LED_FLAG_COM
  *            @arg LED_FLAG_FRAME 
  * @retval None
  */
void LED_ClearFlag(uint16_t LED_Flag)
{
    /* Check the parameters */
    assert_param(IS_LED_FLAG(LED_Flag));

    LED->FR.V32 |= (LED_Flag << 16);
}

/**
  * @brief  Clears ALL LED driver mould interrupt flag.
  * @param  None 
  * @retval None
  */
void LEDMOULD_ClearFlag(void)
{
    LED->FR.BIT.COMIFC = SH_SET;
    LED->FR.BIT.AUIFC  = SH_SET;
}

/**
  * @brief  LED COM Port Output Control,Valid only if AUEN = 0 .  
  * @param  NewState: state of the LED COM Port.
  *         This parameter can be one of the following values:
  *            @arg SH_ON  (COM / SEG start output (after a com width, reset by hardware))
  *            @arg SH_OFF (disable COM/SEG output)
  * @retval None
  */
void LED_SINGLESCAN_OnOff(CmdState OnOffState)
{
    /* Check the parameters */
    assert_param(IS_CMD_STATE(OnOffState));
    if (OnOffState != SH_OFF)
    {
        /* Enable the LED peripheral */
        LED->COMCR.V32 |= LED_COMCR_COMGO_Msk;
    }
    else
    {
        /* Disable the LED peripheral */
        LED->COMCR.V32 &= (~LED_COMCR_COMGO_Msk);
    }
}
/**
  * @brief  Led continuous scan start output control bit.  
  * @param  NewState: state of the LED peripheral.
  *         This parameter can be one of the following values:
  *            @arg SH_ON     (COM/SEG Start to output)
  *            @arg SH_OFF    (disable COM/SEG output)
  * @retval None
  */
void LED_AU_OnOff(CmdState OnOffState)
{
    /* Check the parameters */
    assert_param(IS_CMD_STATE(OnOffState));

    if (OnOffState != SH_OFF)
    {
        /* Enable the LED peripheral */
        LED->AUCR.V32 |= LED_AUCR_AUGO_Msk;
    }
    else
    {
        /* Disable the LED peripheral */
        LED->AUCR.V32 &= (~LED_AUCR_AUGO_Msk);
    }
}

/**
  * @brief  LED CCCR (constant current source Control register) ON .       
  * @param  LedCCCRSet: set the LED CCCR.
  *         This parameter can be any combination of the following values:
  *            @arg LED_CCCR_CCSS0
  *            @arg LED_CCCR_CCSS1
  *            @arg LED_CCCR_CCSS2
  *            @arg LED_CCCR_CCSS3
  *            @arg LED_CCCR_CCSS4
  *            @arg LED_CCCR_CCSS5
  *            @arg LED_CCCR_CCSS6
  *            @arg LED_CCCR_CCSS7
  * @param  NewState: state of the LED constant current source.
  *         This parameter can be one of the following values:
  *            @arg SH_ON     (constant current source turn on)
  *            @arg SH_OFF    (constant current source turn off)
  * @retval None
  */
void LED_CCCR_OnOff(uint8_t LedCCCRSet, CmdState OnOffState)
{
    uint32_t tempReg = 0;
 
    /* Check the parameters */
    assert_param(IS_CMD_STATE(OnOffState));
    /* check the parameters */
    assert_param(IS_LED_CCCR(LedCCCRSet));
 
    tempReg = LED->CCCR.V32;
    tempReg &= ~LedCCCRSet;
 
    if (OnOffState != SH_OFF)
    {
        tempReg |= LedCCCRSet;
    }
		
    LED->CCCR.V32 = tempReg;
}

/**
  * @brief  LED CCDR (constant current source data register) select .  
  * @param  LED_COM: LED COM Select.
  *         This parameter can be one of the following values:
  *            @arg LED_COM0 
  *            @arg LED_COM1
  *            @arg LED_COM2
  *            @arg LED_COM3
  *            @arg LED_COM4
  *            @arg LED_COM5
  *            @arg LED_COM6
  *            @arg LED_COM7     
  * @param  LedCCCRSet: set the LED CCDR.
  *         This parameter can be one of the following values:
  *            @arg LED_CCDR_8mA 
  *            @arg LED_CCDR_10mA
  *            @arg LED_CCDR_12mA
  *            @arg LED_CCDR_14mA
  *            @arg LED_CCDR_16mA
  *            @arg LED_CCDR_18mA
  *            @arg LED_CCDR_20mA
  *            @arg LED_CCDR_22mA
  *            @arg LED_CCDR_24mA
  *            @arg LED_CCDR_26mA
  *            @arg LED_CCDR_28mA
  *            @arg LED_CCDR_30mA
  *            @arg LED_CCDR_32mA
  * @retval None
  */
void LED_CCDR_VALESEL(uint8_t LED_COM, LEDCCDR_VALUE_TypeDef LedCCDRSel)
{
    uint8_t comPos = 0;
    uint16_t currentCom = 0;
 
    /* check the parameters */
    assert_param(IS_LEDCOM_MODE(LED_COM));
    assert_param(IS_LED_CCDR_VALUE(LedCCDRSel));

    while ((LED_COM >> comPos) != 0x00)
    {
        currentCom = (uint16_t)0x01 << comPos;
        if ((LED_COM & currentCom) == currentCom)
        {
            LED->CCDR.V32 &= (~((uint32_t)0x0F << (comPos * 4)));
            LED->CCDR.V32 |= ((uint32_t)LedCCDRSel << (comPos * 4));
        }
        comPos++;
    }
}

/**
  * @brief  LED CCCR (constant current source data register) all reset  .       
  * @param  None
  * @retval None
  */
void LED_CCDR_ALLRESET(void)
{
    LED->CCDR.V32 = 0X66666666;
}

/**
  * @brief  LED SEGMOD switch control register .       
  * @param  LedCCCRSet: set the LED CCCR.
  *         This parameter can be any combination of the following values:
  *            @arg LED_SEGMOD_S0MS_SEG  
  *            @arg LED_SEGMOD_S1MS_SEG  
  *            @arg LED_SEGMOD_S2MS_SEG  
  *            @arg LED_SEGMOD_S3MS_SEG  
  *            @arg LED_SEGMOD_S4MS_SEG  
  *            @arg LED_SEGMOD_S5MS_SEG  
  *            @arg LED_SEGMOD_S6MS_SEG  
  *            @arg LED_SEGMOD_S7MS_SEG  
  *            @arg LED_SEGMOD_S8MS_SEG  
  *            @arg LED_SEGMOD_S9MS_SEG  
  *            @arg LED_SEGMOD_S10MS_SEG 
  *            @arg LED_SEGMOD_S11MS_SEG 
  *            @arg LED_SEGMOD_S12MS_SEG 
  *            @arg LED_SEGMOD_S13MS_SEG 
  *            @arg LED_SEGMOD_S14MS_SEG 
  *            @arg LED_SEGMOD_S15MS_SEG 
  * @retval None
  */
void LED_SEGMOD_SEGSEL(uint16_t LedSEGSEL)
{
    /* check the parameters */
    assert_param(IS_LED_SEGMOD(LedSEGSEL));

    LED->SEGMOD.V32 |= LedSEGSEL;
}

/**
  * @brief  LED SEGMOD switch control register all reset.       
  * @param  None
  * @retval None
  */
void LED_SEGMODE_ALLRESET(void)
{
    LED->SEGMOD.V32 = 0X00000000;
}

/**
  * @brief  LED contiue scanf SEG ouput control.       
  * @param  AUSORXCOMSEL: LED COMCR COMSEL mode seclet.
  *         This parameter can be any combination of the following values:
  *            @arg LED_COMCR_COM0  
  *            @arg LED_COMCR_COM1  
  *            @arg LED_COMCR_COM2  
  *            @arg LED_COMCR_COM3  
  *            @arg LED_COMCR_COM4  
  *            @arg LED_COMCR_COM5  
  *            @arg LED_COMCR_COM6  
  *            @arg LED_COMCR_COM7
  * @param  SEGSEL: AU SEG output control select.
  *         This parameter can be any combination of the following values:
  *            @arg LED_SEG_SOR_IO  
  *            @arg LED_SEG_SOR_SEG  
  * @retval None
  */
void LED_AUSORX_COMSEL(LEDCOMCR_COMSEL_TypeDef AUSORXCOMSEL, uint16_t SEGSEL)
{
    /* Check the parameters */
    assert_param(IS_LEDCOMCR_MODE(AUSORXCOMSEL));

    if (AUSORXCOMSEL == LED_COMCR_COM0)
        LED->SOR0.BIT.COM0SEG = SEGSEL;
    else if (AUSORXCOMSEL == LED_COMCR_COM1)
        LED->SOR0.BIT.COM1SEG = SEGSEL;
    else if (AUSORXCOMSEL == LED_COMCR_COM2)
        LED->SOR1.BIT.COM2SEG = SEGSEL;
    else if (AUSORXCOMSEL == LED_COMCR_COM3)
        LED->SOR1.BIT.COM3SEG = SEGSEL;
    else if (AUSORXCOMSEL == LED_COMCR_COM4)
        LED->SOR2.BIT.COM4SEG = SEGSEL;
    else if (AUSORXCOMSEL == LED_COMCR_COM5)
        LED->SOR2.BIT.COM5SEG = SEGSEL;
    else if (AUSORXCOMSEL == LED_COMCR_COM6)
        LED->SOR3.BIT.COM6SEG = SEGSEL;
    else if (AUSORXCOMSEL == LED_COMCR_COM7)
        LED->SOR3.BIT.COM7SEG = SEGSEL;
}

/**
  * @brief  LED contiue scanf SEG ouput control.       
  * @param  LEDSDRX: LED SDRx value set.
  *         This parameter can be any combination of the following values:
  *            @arg LED_SDR0_SEG0  
  *            @arg LED_SDR0_SEG1  
  *            @arg LED_SDR0_SEG2  
  *            @arg LED_SDR0_SEG3  
  *            @arg LED_SDR1_SEG4  
  *            @arg LED_SDR1_SEG5  
  *            @arg LED_SDR1_SEG6  
  *            @arg LED_SDR1_SEG7 
  *            @arg LED_SDR2_SEG8 
  *            @arg LED_SDR2_SEG9 
  *            @arg LED_SDR2_SEG10
  *            @arg LED_SDR2_SEG11
  *            @arg LED_SDR3_SEG12
  *            @arg LED_SDR3_SEG13
  *            @arg LED_SDR3_SEG14
  *            @arg LED_SDR3_SEG15
  * @param  SDRXSEL: SEG width select bit.   
  * @retval None
  */
void LED_SDRxSET(LEDSDRX_SEG_TypeDef LEDSDRX, uint8_t SDRXSEL)
{
    /* Check the parameters */
    assert_param(IS_LEDSDR_MODE(LEDSDRX));

    if (LEDSDRX == LED_SDR0_SEG0)
        LED->SDR0.BIT.SEG0DUTY = SDRXSEL;
    else if (LEDSDRX == LED_SDR0_SEG1)
        LED->SDR0.BIT.SEG1DUTY = SDRXSEL;
    else if (LEDSDRX == LED_SDR0_SEG2)
        LED->SDR0.BIT.SEG2DUTY = SDRXSEL;
    else if (LEDSDRX == LED_SDR0_SEG3)
        LED->SDR0.BIT.SEG3DUTY = SDRXSEL;
    else if (LEDSDRX == LED_SDR1_SEG4)
        LED->SDR1.BIT.SEG4DUTY = SDRXSEL;
    else if (LEDSDRX == LED_SDR1_SEG5)
        LED->SDR1.BIT.SEG5DUTY = SDRXSEL;
    else if (LEDSDRX == LED_SDR1_SEG6)
        LED->SDR1.BIT.SEG6DUTY = SDRXSEL;
    else if (LEDSDRX == LED_SDR1_SEG7)
        LED->SDR1.BIT.SEG7DUTY = SDRXSEL;
    else if (LEDSDRX == LED_SDR2_SEG8)
        LED->SDR2.BIT.SEG8DUTY = SDRXSEL;
    else if (LEDSDRX == LED_SDR2_SEG9)
        LED->SDR2.BIT.SEG9DUTY = SDRXSEL;
    else if (LEDSDRX == LED_SDR2_SEG10)
        LED->SDR2.BIT.SEG10DUTY = SDRXSEL;
    else if (LEDSDRX == LED_SDR2_SEG11)
        LED->SDR2.BIT.SEG11DUTY = SDRXSEL;
    else if (LEDSDRX == LED_SDR3_SEG12)
        LED->SDR3.BIT.SEG12DUTY = SDRXSEL;
    else if (LEDSDRX == LED_SDR3_SEG13)
        LED->SDR3.BIT.SEG13DUTY = SDRXSEL;
    else if (LEDSDRX == LED_SDR3_SEG14)
        LED->SDR3.BIT.SEG14DUTY = SDRXSEL;
    else if (LEDSDRX == LED_SDR3_SEG15)
        LED->SDR3.BIT.SEG15DUTY = SDRXSEL;
}

/**
  * @brief  LED contiue scanf SEG ouput control all reset.       
  * @param  None
  * @retval None
  */
void LED_AUSORX_ALLRESET(void)
{
    LED->SOR0.V32 = 0X00000000;
    LED->SOR1.V32 = 0X00000000;
    LED->SOR2.V32 = 0X00000000;
    LED->SOR3.V32 = 0X00000000;
}

/**
  * @brief  Enable or disable the  LED AU frame interrupt.       
  * @param  NewState: new state of the LED frame interrupt.
  *         This parameter can be one of the following values:
  *            @arg SH_ENABLE
  *            @arg SH_DISABLE
  * @retval None
  */
void LED_INT_AUFrameConfig(FunctionalState NewState)
{
    /* check the parameters */
    assert_param(IS_FUNCTION_STATE(NewState));

    if (NewState != SH_DISABLE)
    {
        LED->AUCR.V32 |= LED_AUCR_AUIE_Msk;
    }
    else
    {
        LED->AUCR.V32 &= (~LED_AUCR_AUIE_Msk);
    }
}

/**
  * @brief  Enable or disable the  LED COM interrupt.       
  * @param  NewState: new state of the LED com interrupt.
  *         This parameter can be one of the following values:
  *            @arg SH_ENABLE
  *            @arg SH_DISABLE
  * @retval None
  */
void LED_INT_ComConfig(FunctionalState NewState)
{
    /* check the parameters */
    assert_param(IS_FUNCTION_STATE(NewState));

    if (NewState != SH_DISABLE)
    {
        LED->COMCR.V32 |= LED_COMCR_COMIE_Msk;
    }
    else
    {
        LED->COMCR.V32 &= (~LED_COMCR_COMIE_Msk);
    }
}

/**
  * @brief  Enable or disable the AU LED frame DMA.       
  * @param  NewState: new state of the LED frame DMA.
  *         This parameter can be one of the following values:
  *            @arg SH_ENABLE
  *            @arg SH_DISABLE
  * @retval None
  */
void LED_DMA_AUFrameEN(FunctionalState NewState)
{
    /* check the parameters */
    assert_param(IS_FUNCTION_STATE(NewState));

    if (NewState != SH_DISABLE)
    {
        LED->AUCR.V32 |= LED_AUCR_AUDMA_Msk;
    }
    else
    {
        LED->AUCR.V32 &= (~LED_AUCR_AUDMA_Msk);
    }
}

/**
  * @brief  Enable or disable the  LED COM DMA.       
  * @param  NewState: new state of the LED com DMA.
  *         This parameter can be one of the following values:
  *            @arg SH_ENABLE
  *            @arg SH_DISABLE
  * @retval None
  */
void LED_DMA_ComEN(FunctionalState NewState)
{
    /* check the parameters */
    assert_param(IS_FUNCTION_STATE(NewState));

    if (NewState != SH_DISABLE)
    {
        LED->COMCR.V32 |= LED_COMCR_LEDCDMA_Msk;
    }
    else
    {
        LED->COMCR.V32 &= (~LED_COMCR_LEDCDMA_Msk);
    }
}
/**
  * @brief  Check whether the  LED COM flag is set or not.
  * @param  LEDMOUDLE: LED mould instruct.
  * @retval The new state of LED's flag.
  */
FlagStatus LED_GetComFlagStatus(LED_TypeDef *LEDMOUDLE)
{
    FlagStatus bitStatus;

    if ((LEDMOUDLE->FR.V32 & LED_FR_COMIF_Msk) != SH_RESET)
    {
        bitStatus = SH_SET;
    }
    else
    {
        bitStatus = SH_RESET;
    }

    return bitStatus;
}

/**
  * @brief  Check whether the specified LED Frame flag is set or not.
  * @param  LEDMOUDLE: LED mould instruct.
  * @retval The new state of LED's flag.
  */
FlagStatus LED_GetFrameFlagStatus(LED_TypeDef *LEDMOUDLE)
{
    FlagStatus bitStatus;

    if ((LEDMOUDLE->FR.V32 & LED_FR_AUIF_Msk) != SH_RESET)
    {
        bitStatus = SH_SET;
    }
    else
    {
        bitStatus = SH_RESET;
    }

    return bitStatus;
}
/**
  * @}
  */

/**
  * @}
  */

/************************ (C) COPYRIGHT Sinowealth *****END OF FILE****/
